<?php include("header.php");
include("geocoder.php");
//echo "in new register";
$org_name =  mysql_real_escape_string($_POST["org"]);//todo: add orgName
$first_name =  mysql_real_escape_string($_POST["first_name"]);
$last_name =  mysql_real_escape_string($_POST["last_name"]);
$area_code = mysql_real_escape_string($_POST["phone_num1"]);
$phone_num = mysql_real_escape_string($_POST["phone_num2"]);
$phone_num2 = mysql_real_escape_string($_POST["phone_num3"]);
$fax_code = mysql_real_escape_string($_POST["fax_num1"]);//add fax
$fax_num = mysql_real_escape_string($_POST["fax_num2"]);//add
$fax_num2 = mysql_real_escape_string($_POST["fax_num3"]);//add
$street_address = mysql_real_escape_string($_POST["street_address"]);
$second_line = mysql_real_escape_string($_POST["line2"]);
$city = mysql_real_escape_string($_POST["city"]);
$zipcode = mysql_real_escape_string($_POST["zipcode"]);
$email =  mysql_real_escape_string($_POST["email"]);
//$post_until = $_POST["month"] + "/" + $_POST["day"] + "/" + $_POST["year"];
//$post_until =  mysql_real_escape_string($post_until);
$times =  mysql_real_escape_string($_POST["times"]);
//$tree_info =  mysql_real_escape_string($_POST["tree_info"]);
$add_info =  mysql_real_escape_string($_POST["add_info"]);
$equipment = "";

$category = mysql_real_escape_string($_POST["category"]);
$website = mysql_real_escape_string($_POST["website"]);//website
//$password = mysql_real_escape_string($_POST["password"]);


$address = $street_address." ".$second_line." CA ".$zipcode;
$geocodeAddress = preg_replace('/\s+/', '+', $address);
$query = <<<END
	SELECT MAX(`orgID`) FROM `hestia`.`organizations`;
END;

$result = mysql_query($query, $connection);

if ($result == false){
	//mysql_close($connection);
	die("Registration Failed:".mysql_error($connection));
}

$result = mysql_fetch_array($result);

$geocode = geocode($geocodeAddress);
if($geocode != false){
	$lat = $geocode["lat"];
	$lng = $geocode["lng"];
}
else 
	die("address doest not exist");


$query = <<<END
	INSERT INTO `hestia`.`organizations`
	(`orgName`,
	 `firstName`,`lastName`, 
	 `areaCode`,`prefix`, `lineNumber`,
	 `faxAreaCode`,`faxPrefix`, `faxLineNumber`,  
	 `streetAddress`, `streetAddress2`, `city`, `zipcode`, 
	 `websiteAddress`,`email`, 
	 `times`,
	 `additionalInfo`,
	 `lat`, `lng`,
	 `category`)
	VALUES (
		'$org_name',
		'$first_name','$last_name',
		'$area_code','$phone_num','$phone_num2',
		'$fax_code','$fax_num','$fax_num2',
		'$street_address','$second_line','$city', '$zipcode',
		'$website','$email', 
		'$times',
		'$add_info',
		'$lat', '$lng',
		'$category');
END;
$result = mysql_query ( $query, $connection );

if ($result) {
	$query = "SELECT MAX(orgID) FROM `organizations`";
	$result =mysql_query ( $query, $connection );
	
	echo mysql_error($connection); 
	
	$result = mysql_fetch_array($result);
	$query = "SELECT * FROM `organizations` WHERE `orgID` = $result[0]"; 
	$result =mysql_query ( $query, $connection );
	
	echo mysql_error($connection); 
	
	if ($result) {
		//$result = mysql_fetch_array($result);
		$output = <<<END
		<script type="text/javascript" src="jscripts/view.js"></script>
<script type="text/javascript" src="jscripts/calendar.js"></script>
<body id="form">


<div id="form_container">
<form id="form_158019" class="appnitro" method="post" action="new_register_submit.php">
<div class="form_description">
<h2>Organization Registration Successful</h2>
<p>Thank you  for registering with Anteater Feeder</p>
</div>
</form>
</div>
</body>
END;
		echo $output;
	}
}
else
	die("Registration Failed:".mysql_error($connection)); 
 include("footer.php"); ?>